#include "Timer.h"
#include <iostream>

Timer::Timer(int timeInterval)
{
    UpdateSysTimeInterval(timeInterval);
    StartTask();        // 启动线程任务
}

Timer::~Timer()
{
    m_beStop = true;
    m_cond.NotifyForVariable();
}

int Timer::DoTask()
{
    while (!m_beStop)
    {
        HandleTimer();

        m_cond.WaitForVariable(m_timeInterval);
    }
    std::cout << "Timer::DoTask() stop" << std::endl;
    return 0;
}


bool Timer::UpdateSysTimeInterval(int timeInterval)
{
    if (IsLegalTimeInterval(timeInterval))
    {
        m_timeInterval.store(timeInterval);
        std::cout << "Timer::UpdateSysTimeInterval() new timeInterval = " 
            << timeInterval << std::endl;

        return true;
    }
    return false;
}

bool Timer::IsLegalTimeInterval(int timeInterval)
{
    if (timeInterval < MIN_TIME_INTERVAL)
    {
        return false;
    }
    return true;
}


bool Timer::AddTimer(TimerData timerData)
{
    std::lock_guard<std::mutex> lock(m_mutex);

    auto it = m_timerDataMap.find(timerData.id);
    if (it != m_timerDataMap.end())
    {
        std::cout << "Timer::AddTimer() id = " << timerData.id << " is exist" << std::endl;
        return false;
    }

    if (timerData.timeInterval < m_timeInterval.load())
    {
#if 0   // 如果提供的间隔时间小于最小间隔时间，就不添加timer任务
        std::cout << "Timer::AddTimer() id = " << timerData.id << 
            ", timeInterval = " << timerData.timeInterval << " is not legal" << std::endl;
        return false;

#else   // 如果提供的间隔时间小于最小间隔时间，修改任务的最小间隔时间为系统最小间隔时间
        timerData.timeInterval = m_timeInterval.load();
#endif
    }

    m_timerDataMap[timerData.id] = timerData;
    std::cout << "Timer::AddTimer() id = " << timerData.id << 
            ", timeInterval = " << timerData.timeInterval << std::endl;
    return true;
}

bool Timer::UpdateTimer(const std::string& id, int timeInterval)
{
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_timerDataMap.find(id) == m_timerDataMap.end())
    {
        std::cout << "Timer::UpdateTimer() id = " << id << " is not exist" << std::endl;
        return false;
    }
    if (timeInterval >= m_timeInterval.load())
    {
        m_timerDataMap[id].timeInterval = timeInterval;
        m_timerDataMap[id].countl = 0;
        std::cout << "Timer::UpdateTimer() id = " << id << 
            ", timeInterval = " << timeInterval << std::endl;

        return true;
    }
    return false;
}

bool Timer::RemoveTimer(const std::string& id)
{
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_timerDataMap.find(id) == m_timerDataMap.end())
    {
        std::cout << "Timer::RemoveTimer() id = " << id 
            << " is not exist" << std::endl;
            
        return false;
    }
    m_timerDataMap.erase(id);
    return true;
}

void Timer::RemoveAllTimer()
{
    std::lock_guard<std::mutex> lock(m_mutex);
    m_timerDataMap.clear();
}

void Timer::HandleTimer()
{
    std::lock_guard<std::mutex> lock(m_mutex);
    for (auto& it : m_timerDataMap)
    {

#if 0   // 先等待一个间隔时间，再执行任务
        it.second.countl++;
        if ((it.second.countl * m_timeInterval.load()) >= it.second.timeInterval)
        {
            it.second.countl = 0;
            it.second.func();       // 执行任务
        }

#else   // 立即执行任务

        if (it.second.countl == 0)  // 第一次进来就执行任务
        {
            it.second.func();       // 执行任务
        }

        it.second.countl++;
        if ((it.second.countl * m_timeInterval.load()) >= it.second.timeInterval)
        {
            it.second.countl = 0;
        }
#endif

    }
}
